#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>

#define USER_DIR "users"

int menu()
{
    int n;
    printf("****欢迎使用ATM机****\n");
    printf("\t1.开户\n");
    printf("\t2.登录\n");
    printf("*********************\n");
    printf("输入要选择的功能：");
    scanf("%d", &n);
    return n;
}
void menu_kaihu()
{
    char name[50];
    int pwd;
    printf("********开户********\n");
    printf("请输入用户名：");
    scanf("%s", &name);
    printf("请输入密码：");
    scanf("%d", &pwd);
    kaihu(name, pwd);
}
void menu_denglu()
{
    char name[50];
    int pwd;
    printf("********登录********\n");
    printf("请输入用户名：");
    scanf("%s", &name);
    printf("请输入密码：");
    scanf("%d", &pwd);
    denglu(name, pwd);
}
void kaihu(char name[50], int pwd)
{
    if (!isUserExist(name))
    {
        printf("用户已经存在，无法再次开户！！\n");
        return;
    }
    char buf[100];
    double money = 0;
    mkdir(USER_DIR, 777);
    sprintf(buf, "%s/%s.od", USER_DIR, name);
    int fd = open(buf, O_RDWR | O_CREAT, 777);
    write(fd, &pwd, sizeof(int));
    write(fd, &money, sizeof(double));
}
void denglu(char name[50], int pwd)
{
    if (isUserExist(name))
    {
        printf("用户不存在！！\n");
        return;
    }
    char buf[100];
    int p;
    double money;
    sprintf(buf, "%s/%s.od", USER_DIR, name);
    int fd = open(buf, O_RDWR | O_CREAT, 777);
    read(fd, &p, sizeof(int));
    if (strcmp(&pwd, &p))
    {
        printf("密码错误！！\n");
        return;
    }
    while (1)
    {
        switch (menu2())
        {
        case 1:
            chaxun(fd);
            break;
        case 2:
            printf("请输入要存入的金额：");
            scanf("%lf", &money);
            cunkuan(fd, money);
            break;
        case 3:
            printf("请输入要取出的金额：");
            scanf("%lf", &money);
            if (!qukuan(fd, money))
            {
                printf("取款失败，账户余额不足！！\n");
            }
            break;
        case 4:
            zhuanzhang(fd);
            break;
        case 5:
            xiaohu(name);
            return;
        default:
            return;
        }
    }
}
void chaxun(int fd)
{
    double money;
    lseek(fd, sizeof(int), SEEK_SET);
    read(fd, &money, sizeof(double));
    printf("余额为：%.2lf 元\n", money);
}
void cunkuan(int fd, double money)
{
    double n;
    lseek(fd, sizeof(int), SEEK_SET);
    read(fd, &n, sizeof(double));
    n = n + money;
    lseek(fd, sizeof(int), SEEK_SET);
    write(fd, &n, sizeof(double));
}
int qukuan(int fd, double money)
{
    double n;
    lseek(fd, sizeof(int), SEEK_SET);
    read(fd, &n, sizeof(double));
    if (n < money)
    {
        return 0;
    }
    n = n - money;
    lseek(fd, sizeof(int), SEEK_SET);
    write(fd, &n, sizeof(double));
}
void zhuanzhang(int fd)
{
    char name[50], buf[100];
    int fd2;
    double money;
    printf("请输入对方用户名：");
    scanf("%s", &name);
    if (isUserExist(name))
    {
        printf("用户不存在，无法转账！！\n");
        return;
    }
    printf("请输入要转账的金额：");
    scanf("%lf", &money);
    if (!qukuan(fd, money))
    {
        printf("转账失败，账户余额不足！！\n");
        return;
    }
    sprintf(buf, "%s/%s.od", USER_DIR, name);
    fd2 = open(buf, O_RDWR | O_CREAT, 777);
    cunkuan(fd2, money);
}
void xiaohu(char name[50])
{
    char dir[50];
    sprintf(dir, "rm -f %s/%s.od", USER_DIR, name);
    system(dir);
}
int isUserExist(char name[50])
{
    char buf[100];
    sprintf(buf, "%s/%s.od", USER_DIR, name);
    return access(buf, F_OK);
}
int menu2()
{
    int n;
    printf("****欢迎使用ATM机****\n");
    printf("\t1.查询\n");
    printf("\t2.存款\n");
    printf("\t3.取款\n");
    printf("\t4.转账\n");
    printf("\t5.销户\n");
    printf("*********************\n");
    printf("输入要选择的功能：");
    scanf("%d", &n);
    return n;
}
int main()
{
    signal(SIGINT, SIG_IGN);
    while (1)
    {
        switch (menu())
        {
        case 1:
            menu_kaihu();
            break;
        case 2:
            menu_denglu();
            break;
        default:
            return;
        }
    }
}
